package com.packenius.library.xspdf;

import java.util.HashMap;
import java.util.Map;

/**
 * Mapping between unicode character and character name.
 * 
 * @author Christian Packenius, 2013.
 */
public class XSUnicodeMapping implements XSUnicode, XSUnicodeZapfDingbats {
  /**
   * Maps from Unicode to Adobe glyph fontType.
   */
  private static final Map<Character, String> mapCode2Name = new HashMap<Character, String>();

  /**
   * Maps from Adobe glyph fontType to Unicode.
   */
  private static final Map<String, Character> mapName2Code = new HashMap<String, Character>();

  static {
    // Create "usually" character mappings.
    for (int i = 0; i < unicodes.length; i += 2) {
      String name = unicodes[i];
      char code = unicodes[i + 1].charAt(0);
      mapCode2Name.put(code, name);
      mapName2Code.put(name, code);
    }

    // Create additional ZapfDingbats character mappings.
    for (int i = 0; i < zapfDingbatsCodes.length; i += 2) {
      String name = zapfDingbatsCodes[i];
      char code = zapfDingbatsCodes[i + 1].charAt(0);
      if (mapCode2Name.get(code) == null) {
        mapCode2Name.put(code, name);
      }
      mapName2Code.put(name, code);
    }
  }

  /**
   * Returns the character name of a unicode character.
   * 
   * @param unicodeCode
   * @return Character name.
   */
  public static String getNameFromCode(char unicodeCode) {
    return mapCode2Name.get(unicodeCode);
  }

  /**
   * Returns the character name of a unicode character.
   * 
   * @param characterName Name of the character.
   * @return Character name.
   */
  public static Character getCodeFromName(String characterName) {
    return mapName2Code.get(characterName);
  }
}
